package adger.me.yili.view.impl;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.orhanobut.logger.Logger;

import net.soulwolf.widget.speedyselector.widget.SelectorEditText;
import net.soulwolf.widget.speedyselector.widget.SelectorTextView;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.Date;

import adger.me.yili.R;
import adger.me.yili.bean.Token;
import adger.me.yili.bean.User;
import adger.me.yili.utils.DB;
import adger.me.yili.utils.SharePreferencesUtil;
import adger.me.yili.utils.UriCenter;
import adger.me.yili.utils.XHttp;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {
    @Bind(R.id.set_username)
    SelectorEditText set_username;
    @Bind(R.id.set_password)
    SelectorEditText set_password;
    DbManager dbManager;
    @Bind(R.id.login_btn)
    SelectorTextView login_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        dbManager = DB.getXInstance();
        Token cacheToken = null;
        try {
            cacheToken = dbManager.selector(Token.class).findFirst();
        } catch (DbException e) {
            e.printStackTrace();
        }

        if (cacheToken != null) {
            long nowTime = new Date().getTime();
            String now = Long.toString(nowTime);
            long curretTime = Long.parseLong(now.substring(0, 10));
            long expire = cacheToken.getExpire();
            // 如果当前事件大于expire的事件,需要重新登录获取token
            if (curretTime < expire) {
                goNextIntent();
            }
        }
        initView();
    }

    private void initView() {

        set_username.setText(SharePreferencesUtil.getSpString("activityTitle", "", LoginActivity.this));
        set_password.setText(SharePreferencesUtil.getSpString("userPassword", "", LoginActivity.this));
    }

    private void goNextIntent() {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @OnClick(R.id.login_btn)
    public void onClick() {
        login_btn.setEnabled(false);
        User user = new User();
        user.setUsername(set_username.getText().toString().trim());
        user.setPassword(set_password.getText().toString().trim());
        XHttp.post(UriCenter.login, user, new XHttp.XCallback<Token>() {
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                login_btn.setEnabled(true);
            }

            @Override
            public void onSuccess(Token result) {
                Logger.d(result.getError()+"  ");
                switch (result.getError()) {

                    case 0:
                        try {
                            dbManager.saveOrUpdate(result);
                            SharePreferencesUtil.putSpString("histryToken", result.getToken(), LoginActivity.this);
                            SharePreferencesUtil.putSpString("activityTitle", set_username.getText().toString().trim(), LoginActivity.this);
                            SharePreferencesUtil.putSpString("userPassword", set_password.getText().toString().trim(), LoginActivity.this);
                            goNextIntent();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }
                        break;
                    case 102:
                        Toast.makeText(LoginActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
                        login_btn.setEnabled(true);
                        break;
                }
            }
        });
    }
}
